<?php
namespace app\single\controller;
/**
 * #优惠卷 {关于用户优惠卷相关}
 */
class Coupon extends Common{
    /**
     * 获取所有通用优惠卷列表
     * @return [type] [description]
     */
    function index(){
        $list=model($this->table)->readAll(['status'=>1,'type'=>0]);
        $time=time();
        $myList=model('memberCoupon')->readAll([
            'status'=>1,
            'uid'=>$this->user['id'],
        ],0,['field'=>'cid,e_time']);

        if(!empty($myList)){
            foreach ($myList as $v) {
                foreach ($list as $k => $c) {
                    if($v['cid']===$c['id']){
                        $list[$k]['state']=$v['e_time']<$time?1:2;
                        break;
                    }
                }
            }
        }
        return json('',0,$list);
    }
    /**
     * 获取所有用户已领取的优惠卷
     * @return [type] [description]
     */
    function memberCoupon(){
        $time=time();
        $list=model('memberCoupon')->readAll([
            'status'=>1,
            'e_time'=>['e_time','>',$time],
            'uid'=>$this->user['id']
        ],0,[
            'field'=>'id,title,usable,value,e_time,remark'
        ]);
        // 是否删除过期的优惠卷
        return json('',0,$list);
    }
    /**
     * 领取优惠卷
     * @param  integer $cid 优惠卷ID
     * @return [type]       [description]
     */
    function accept($cid=0,$from='phone'){
        $uid=$this->user['id'];
        return model($this->table)->accept($uid,$cid,$from);
    }
}